package com.thejupitech;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


public class LazyAdapter extends BaseAdapter {
	private Activity activity;
	private ArrayList<HashMap<String, String>> bookList;
	private static LayoutInflater inflater=null;
	public ImageLoader imageLoader;	
	
	public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> bookList) {
		activity = a;
		this.bookList=bookList;
		inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader=new ImageLoader(activity.getApplicationContext());
	}	
	public int getCount() {
		return bookList.size();
	}

	public Object getItem(int position) {
		return position;
	}


	public long getItemId(int position) {
		return position;
	}
	public View getView(final int position, View convertView, ViewGroup parent) {
		View vi = convertView;
			BooksWrapper booksWrapper;
			if(vi == null) {
				vi = inflater.inflate(R.layout.view_books_layout, null);
				booksWrapper = new BooksWrapper(vi);
				vi.setTag(booksWrapper);
			} else {
				booksWrapper = (BooksWrapper)vi.getTag();
			}
			
			
	        booksWrapper.getIsbn().setText(bookList.get(position).get("isbn"));
	        booksWrapper.getBookTitle().setText(bookList.get(position).get("title"));
	        booksWrapper.getAuthor().setText(bookList.get(position).get("author"));
	        imageLoader.DisplayImage(bookList.get(position).get("image"), booksWrapper.getBookIcon());				
        return vi;
	}

}
